import 'package:favoritecate/core/model/category_model.dart';
import 'package:flutter/material.dart';
import 'package:favoritecate/core/services/json_parse.dart';
import 'home_category_Item.dart';

// 123.207.32.32:8001/api/meal
class HomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<CategoryModel>>(
      future: JsonParse.loadCategoryJson(),
      builder: (ctx,snapshot){
        if(!snapshot.hasData){
          //没有数据时
          return Center(child: CircularProgressIndicator());
        }

        if(snapshot.error != null){
          return Center(
            child: Text('数据异常'),
          );
        }

        final _list = snapshot.data;
        return GridView.builder(
          padding: EdgeInsets.all(20),
          itemCount: _list.length,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              crossAxisSpacing: 20 ,
              mainAxisSpacing: 20,
              childAspectRatio: 1.5
          ),
          itemBuilder:(ctx,index){
            return FYHomeCategoryItem(_list[index]);
          },
        );
      },
    );
  }
}


//class HomeContent extends StatefulWidget {
//  @override
//  _HomeContentState createState() => _HomeContentState();
//}
//
//class _HomeContentState extends State<HomeContent> {
//
//  List<CategoryModel> _list = [];
//
//  @override
//  void initState() {
//    // TODO: implement initState
//    super.initState();
//    //加载数据
//    JsonParse.loadCategoryJson().then((list){
//      setState(() {
//        _list = list;
//      });
//    });
//  }
//
//  @override
//  Widget build(BuildContext context) {
//    return GridView.builder(
//      padding: EdgeInsets.all(20),
//      itemCount: _list.length,
//        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
//            crossAxisCount: 2,
//            crossAxisSpacing: 20 ,
//          mainAxisSpacing: 20,
//          childAspectRatio: 1.5
//        ),
//        itemBuilder:(ctx,index){
//          return FYHomeCategoryItem(_list[index]);
//        },
//    );
//  }
//}

